package com.screw.carnival.sdk.utils;

import com.screw.carnival.sdk.ad.SCarnivalMaxReward;
import com.screw.carnival.sdk.ad.SCarnivalVideoIAP;
import com.screw.carnival.sdk.ad.SCarnivalVideoManger;
import com.screw.carnival.sdk.ad.SCarnivalVideoRewardCallback;
import com.screw.carnival.sdk.bridge.SCarnivalCocosBridge;

import org.json.JSONObject;

public class ADUtils {

    public static void playAd(String adSource) {
        try {
            SCarnivalVideoIAP adService = null;
            boolean isMaxReady = SCarnivalMaxReward.getInstance().isReady();
            if (isMaxReady) {
                adService = SCarnivalVideoManger.getReward1Instance();
            }

            if (adService == null) {
                callbackAdError();
                return;
            }

            adService.showAd(SCarnivalCocosBridge.mainGameActivity.get(), new SCarnivalVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    callbackAdOver(adSource);
                }

                @Override
                public void onAdError() {
                    callbackAdError();
                }
            });
        } catch (Exception e) {
            callbackAdError();
        }
    }

    private static void callbackAdOver(String adSource) {
        try {
            JSONObject jsonObject = getJson(adSource);
            SCarnivalCocosBridge.getInstance().XSSdkCallback("ad_over_wh_a", jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void callbackAdError() {
        try {
            SCarnivalCocosBridge.getInstance().XSSdkCallback("ad_error_wh_a", "{}");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static JSONObject getJson(String adSource) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("ad_source", adSource);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return jsonObject;
    }

}
